<?php
global $basic_short_elements_setting;
global $contact_basic_elements_setting;

//Set value variable globar
$basic_short_elements_setting = contact_basic_short_elements_setting();
$contact_basic_elements_setting = contact_basic_elements_setting();


function contact_elements_render($element_key, $element) {
	$grid_group = array("1-cols", "2-cols", "3-cols", "4-cols", "5-cols");
	$input_group = array("text", "select", "textarea", "checkbox", "radio");
	$heading_group = array("h1", "h2", "h3", "h4", "h5", "h6");
	
	$element_render_html = "";
	if (in_array($element_key, $grid_group)) {
		$element_render_html = contact_element_cols_render($element_key, $element);
	} elseif (in_array($element_key, $input_group)) {
		$element_render_html = contact_element_input_render($element_key, $element);
	} elseif ($element_key == "captcha") {
		$element_render_html = contact_element_captcha_render($element_key, $element);
	} elseif ($element_key == "submit") {
		$element_render_html = contact_element_submit_render($element_key, $element);
	} elseif ($element_key == "paragraph") {
		$element_render_html = contact_element_paragraph_render($element_key, $element);
	} elseif ($element_key == "map") {
		$element_render_html = contact_element_map_render($element_key, $element);
	} elseif ($element_key == "social-link") {
		$element_render_html = contact_element_social_link_render($element_key, $element);
	} elseif ($element_key == "image") {
		$element_render_html = contact_element_image_render($element_key, $element);
	} elseif (in_array($element_key, $heading_group)) {
		$element_render_html = contact_element_heading_render($element_key, $element);
	}	
	
	return $element_render_html;
}

function contact_element_cols_render($element_key, $element) {
	global $contact_basic_elements_setting;
	
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";
	
	if ($element_setting !== null) {
		$render_html = "<div class=\"nino-grid-layout-".$element_setting['amount']."\">";
		if ($element == null) {
			$render_html .= '<div class="nino-inputAction">
							<span class="nino-delete fa fa-close" title="delete"></span>
							<span class="nino-move fa fa-arrows" title="move"></span>
						</div>';
		}
		for ($i = 0; $i < $element_setting['amount']; $i++) {
			$render_html .= '<div class="nino-grid-col">';
			if ($element != null && isset($element->colums[$i])) {
				foreach ($element->colums[$i] as $key => $elm) { 
					$render_html .= contact_elements_render($elm->type, $elm);
				}
			}
			$render_html .= '</div>';
		}
		$render_html .= '</div>';
	}
	return $render_html;
}

function contact_element_input_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";
	
	if ($element_setting !== null) {
		$icon_display = $element_setting['input']['icon-display'] ? 'nino-input-icon' : '';
		
		$render_html = <<<EOD
		<div class="nino-formGroup {$icon_display} nino-label-icon nino-icon-right nino-required nino-error1">
			<div class="nino-inputAction">
				<span class="nino-edit fa fa-pencil" title="edit"></span>
				<span class="nino-delete fa fa-close" title="delete"></span>
				<span class="nino-move fa fa-arrows" title="move"></span>
			</div>
			<label for="nino-input-name">
				<span class="nino-labelName"><span class="nino-icon {$element_setting['label']['icon']}"></span>{$element_setting['label']['text']}</span>
				<span class="nino-asterisk">*</span>
				<span class="nino-subLabel">{$element_setting['sub-label']['text']}</span>
			</label>
			<div class="nino-input-wrap">
EOD;

		switch ($element_setting['input']['type']) {
			case "text":
				$render_html .= text_input_render($element_setting);
				break;
			case "select":
				$render_html .= select_input_render($element_setting);
				break;
		   	case "textarea":
				$render_html .= textarea_input_render($element_setting);
				break;
			case "checkbox":
				$render_html .= checkbox_input_render($element_setting);
				break;
			case "radio":
				$render_html .= radio_input_render($element_setting);
				break;
		}
		
		$render_html .= <<<EOD
				<p class="nino-error-message">{$element_setting['validation']['error']}</p>
			</div>							
		</div>
EOD;
	}
	
	return $render_html;
}

function text_input_render($element_setting) {
	return <<<EOD
	<div class="nino-input">
		<span class="nino-icon fa {$element_setting['input']['icon']}"></span>
		<input type="text" placeholder="{$element_setting['input']['placeholder']}" />									
	</div>
EOD;
}

function select_input_render($element_setting) {
	$render = <<<EOD
	<div class="nino-input nino-select-box-style">
		<select>
EOD;
	foreach ($element_setting['input']['options'] as $value => $text) {
		$checked = $element_setting['input']['select'] == $value ? "selected" : "";
		$render .= "<option value=\"{$value}\" {$checked}>{$text}</option>";
	}
	
	$render .= <<<EOD
		</select>
		<i></i>
	</div>
EOD;

	return $render;
}

function textarea_input_render($element_setting) {
	return <<<EOD
	<div class="nino-input">
		<span class="nino-icon fa {$element_setting['input']['icon']}"></span>
		<textarea placeholder="{$element_setting['input']['placeholder']}" ></textarea>									
	</div>
EOD;
}

function checkbox_input_render($element_setting) {
	$render = <<<EOD
	<div class="nino-input">
		<ul class="nino-input-style">
EOD;
	foreach ($element_setting['input']['options'] as $value => $text) {
		$checked = in_array($value, $element_setting['input']['select']) ? "checked=\"checked\"" : "";
		$render .= <<<EOD
		<li>
			<input type="checkbox" value="{$value}" {$checked}/>
			<label for="nino-checkbox-name">{$text}</label>
		</li>
EOD;
	}
	
	$render .= <<<EOD
		</select>
		<i></i>
	</div>
EOD;

	return $render;
}

function radio_input_render($element_setting) {
	$render = <<<EOD
	<div class="nino-input">
		<ul class="nino-input-style">
EOD;
	foreach ($element_setting['input']['options'] as $value => $text) {
		$checked = $element_setting['input']['select'] == $value ? "checked=\"checked\"" : "";
		$render .= <<<EOD
		<li>
			<input type="radio" value="{$value}" name="{$element_setting['input']['name']}" {$checked}/>
			<label for="nino-radio-name">{$text}</label>
		</li>
EOD;
	}
	
	$render .= <<<EOD
		</select>
		<i></i>
	</div>
EOD;

	return $render;
}

function contact_element_captcha_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$icon_display = $element_setting['input']['icon-display'] ? 'nino-input-icon' : '';
		
		$render_html = <<<EOD
		<div class="nino-formGroup nino-input-icon nino-label-icon nino-horizontal">
			<div class="nino-captcha-image"></div>
			<div class="nino-captcha-result">
				<label for="nino-input-captcha">
					<span class="nino-labelName"><span class="nino-icon fa {$element_setting['label']['icon']}"></span>{$element_setting['label']['text']}</span>
					<span class="nino-asterisk">*</span>
					<span class="nino-subLabel">{$element_setting['sub-label']['text']}</span>
				</label>
				<div class="nino-input-wrap">
					<div class="nino-input">
						<span class="nino-icon fa {$element_setting['input']['icon']}"></span>
						<input type="text" placeholder="{$element_setting['input']['placeholder']}" />
						<span class="nino-captcha-refresh fa fa-refresh"></span>
					</div>
					<p class="nino-error-message">{$element_setting['validation']['error']}</p>
				</div>
			</div>
		</div>	
EOD;
	}
	
	return $render_html;
}

function contact_element_submit_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$render_html = <<<EOD
		<div class="nino-formGroup nino-submit-icon">
			<button type="submit" class="nino-button-submit"><span class="nino-icon fa {$element_setting['input']['icon']}"></span>{$element_setting['input']['text']}</button>
		</div>
EOD;
	}
	
	return $render_html;
}

function contact_element_paragraph_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$render_html = <<<EOD
		<div class="nino-formGroup">
			<p class="{$element_setting['input']['class']}">{$element_setting['input']['text']}</p>
		</div>
EOD;
	}
	
	return $render_html;
}

function contact_element_map_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$render_html = <<<EOD
		<div class="nino-formGroup">
			<div class="nino-map-iframe">
				<iframe src="{$element_setting['input']['src']}" width="{$element_setting['input']['width']}" height="{$element_setting['input']['height']}" frameborder="{$element_setting['input']['frameborder']}" style="{$element_setting['input']['style']}"></iframe>
			</div>
		</div>
EOD;
		$render_html = "<div class=\"nino-formGroup\"><div class=\"nino-map-iframe\"> </div></div>";
	}
	
	return $render_html;
}

function contact_element_social_link_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$display_horizontal = $element_setting['input']['display-horizontal'] ? $element_setting['input']['class-horizontal'] : "";
		
		$render_html = "<div class=\"nino-formGroup\"><div class=\"nino-social-link " . $display_horizontal . " clearafter \" >";
		foreach ($element_setting['input']['elements'] as $social) {
			$render_html .= "<a href=\"{$social['href']}\"><span class=\"fa {$social['icon']}\"></span></a>";
		}
			
		$render_html .= '</div></div>';
	}
	
	return $render_html;
}

function contact_element_image_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$render_html = "";
	}
	
	return $render_html;
}

function contact_element_heading_render($element_key, $element) {
	global $contact_basic_elements_setting;
	$element_setting = $contact_basic_elements_setting[$element_key];
	$render_html = "";

	if ($element_setting !== null) {
		$render_html = "<div class=\"nino-formGroup nino-heading-icon\">";
		$render_html .= "<".$element_key." class=\"nino-heading\">";
		if ($element_setting['input']['icon-display']) {
			$render_html .= "<span class=\"nino-icon fa ".$element_setting['input']['icon']."\"></span> ";
		} else {
			$render_html .= "<span class=\"nino-icon fa \"></span> ";
		}
		$render_html .= $element_setting['input']['text']."</".$element_key.">";
		$render_html .= "</div>";
	}
	
	return $render_html;
}